package com.graduation.sports.ui.fragment;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.graduation.sports.R;
import com.graduation.sports.adapter.CommunityAdapter;
import com.graduation.sports.adapter.MyGroupAdapter;
import com.graduation.sports.adapter.SpaceItemDecoration;
import com.graduation.sports.base.BaseFragment;
import com.graduation.sports.bean.CommunityInfo;
import com.graduation.sports.bean.GroupInfo;
import com.graduation.sports.db.DataManager;
import com.graduation.sports.db.RealmHelper;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @ProjectName: sports
 * @Package: com.graduation.sports.ui.fragment
 * @ClassName: CommunityFragment
 * @Description: java类作用描述
 * @Author: APC
 * @CreateDate: 2021/3/2 16:37
 * @Version: 1.0
 */
public class CommunityFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.all_group)
    TextView allGroup;
    @BindView(R.id.recycle_my_group)
    RecyclerView myGroupRecycle;
    @BindView(R.id.recycle_community)
    RecyclerView communityRecycle;

    private MyGroupAdapter groupAdapter;
    private CommunityAdapter communityAdapter;


    private List<GroupInfo> mGroupInfos = new ArrayList<>();
    private List<CommunityInfo> mCommunityInfos = new ArrayList<>();

    public static CommunityFragment newInstance() {
        return new CommunityFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_community;
    }

    @Override
    protected void initEventAndData() {
        allGroup.setOnClickListener(this);

        groupAdapter = new MyGroupAdapter(getContext(), mGroupInfos);
        myGroupRecycle.setLayoutManager(new GridLayoutManager(getContext(), 4));
        myGroupRecycle.addItemDecoration(new SpaceItemDecoration(getResources().getDimensionPixelSize(R.dimen.line)));
        myGroupRecycle.setAdapter(groupAdapter);

        communityAdapter = new CommunityAdapter(getContext(), mCommunityInfos);
        communityRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        communityRecycle.setAdapter(communityAdapter);


        initData();
    }

    private void initData(){
        try {
            mGroupInfos = LitePal.findAll(GroupInfo.class);
            if (null != mGroupInfos) {
                groupAdapter.update(mGroupInfos);
            }
        } catch (Exception e) {
            LogUtils.e("获取小组数据失败", e);
        }

        try {
            mCommunityInfos = LitePal.findAll(CommunityInfo.class);
            if (null != mCommunityInfos) {
                communityAdapter.update(mCommunityInfos);
            }
        } catch (Exception e) {
            LogUtils.e("获取圈子数据失败", e);
         }
    }

    @Override
    public void onClick(View v) {

    }
}
